package com.platform.data.handle.converter.impl;

import com.platform.data.common.CommonConstant;
import com.platform.data.handle.converter.DataTypeEnum;
import com.platform.data.handle.converter.AbstractDataTypeProcessHandler;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;

/**
 * 数组[整数]
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/5/13 16:30
 */
public class ArrayIntegerTypeProcessHandler extends AbstractDataTypeProcessHandler {

    /**
     * 数组[整数]
     *
     * @param typeEnum typeEnum
     * @param fieldName fieldName
     * @param fieldValue fieldValue
     * @return {@link Object}
     */
    @Override
    public Object process(DataTypeEnum typeEnum, String fieldName, String fieldValue) {
        List<Integer> integerList = new ArrayList<>();

        // 若有中文逗号，替换为引文逗号，进行切割
        if(fieldValue.contains(CommonConstant.COMMA_CN_CHARACTER)){
            fieldValue = fieldValue.replace(CommonConstant.COMMA_CN_CHARACTER, CommonConstant.COMMA_EH_CHARACTER);
        }

        String[] splitArray = fieldValue.split(CommonConstant.COMMA_EH_CHARACTER);
        for (String value : splitArray) {
            // 内容匹配冒号（中英文）
            Matcher matcher = super.matcherValue(colonPattern, value);
            if(Objects.nonNull(matcher)){
                String trim = matcher.group(CommonConstant.ONE).trim();

                // 校验是否整数
                matcher = super.matcherValue(integerPattern, trim);
                if(Objects.nonNull(matcher)){
                    integerList.add(Integer.parseInt(trim));
                    continue;
                }
                log.warn("ArrayIntegerTypeProcessHandler.process|属性字段赋值，输入冒号前不是数字，无法转换成整数，属性名称：{}，属性值：{}", fieldName, value);
            }
        }
        return integerList;
    }
}
